//
//  ST_TianButton.swift
//  btvideo
//
//  Created by Jason on 2021/7/19.
//  Copyright © 2021 JarvanZhang. All rights reserved.
//

import UIKit

enum ST_TianButtonLayoutModel {
    /// 外部尺寸固定，只需要最后一个控件(右边/下面控件) 距离父控件的位置约束值
    case outside_stable(lastControlEdgeValue:CGFloat)
    /// 根据内部约束&&Label的文字，自动确定外部控件的尺寸
    case inside_auto_resize(relevantOffset:CGFloat,lastControlEdgeValue:CGFloat)
}



enum ST_TianButtonStyle {
    
    ///【图片左,文字右 布局】
    case image_left_title_right(imageLeft:CGFloat,imageSize:CGSize,layout:ST_TianButtonLayoutModel)
    
    ///【文字左,图片右 布局】
    case title_left_image_right(titleLeft:CGFloat,imageSize:CGSize,layout:ST_TianButtonLayoutModel)
    
    ///【图片上,文字下 布局】
    case image_top_title_bottom(imageTop:CGFloat,imageSize:CGSize,layout:ST_TianButtonLayoutModel)
    
    ///【文字上,图片下 布局】
    case title_top_image_bottom(titleTop:CGFloat,imageSize:CGSize,layout:ST_TianButtonLayoutModel)
}


fileprivate enum ST_TianBtnImageAsset{
    case url(value:URL?)
    case image(img:UIImage)
}

/// 图片 文字布局
class ST_TianButton: UIControl {
    
    init(style:ST_TianButtonStyle){
        
        super.init(frame: .zero)
        self.addSubview(imageView)
        self.addSubview(titleLabel)
        
        switch style {
        case .image_left_title_right(let imageLeft,let imageSize,let layout):
            
            imageView.snp.makeConstraints { make in
                make.left.equalToSuperview().offset(imageLeft)
                make.size.equalTo(imageSize)
                make.centerY.equalToSuperview()
            }
            
            titleLabel.snp.makeConstraints { make in
                
                make.centerY.equalToSuperview()
                switch layout{
                case .outside_stable(let lastControlEdgeValue):
                    make.right.equalToSuperview().offset(lastControlEdgeValue)
                case .inside_auto_resize(let relevantOffset,let lastControlEdgeValue):
                    make.right.equalToSuperview().offset(lastControlEdgeValue)
                    make.left.equalTo(imageView.snp.right).offset(relevantOffset)
                }
            }
            
        case .title_left_image_right(let titleLeft,let imageSize,let layout):
            
            titleLabel.snp.makeConstraints { make in
                make.left.equalToSuperview().offset(titleLeft)
                make.centerY.equalToSuperview()
            }
            
            imageView.snp.makeConstraints { make in
                
                make.centerY.equalToSuperview()
                make.size.equalTo(imageSize)
            
                switch layout{
                case .outside_stable(let lastControlEdgeValue):
                    make.right.equalToSuperview().offset(lastControlEdgeValue)
                case .inside_auto_resize(let relevantOffset,let lastControlEdgeValue):
                    make.right.equalToSuperview().offset(lastControlEdgeValue)
                    make.left.equalTo(titleLabel.snp.right).offset(relevantOffset)
                }
            }
            
        case .image_top_title_bottom(let imageTop,let imageSize,let layout):
            
            imageView.snp.makeConstraints { make in
                make.top.equalToSuperview().offset(imageTop)
                make.size.equalTo(imageSize)
                make.centerX.equalToSuperview()
            }
            
            titleLabel.snp.makeConstraints { make in
                
                switch layout{
                case .outside_stable(let lastControlEdgeValue):
                    make.bottom.equalToSuperview().offset(lastControlEdgeValue)
                    make.centerX.equalToSuperview()
                case .inside_auto_resize(let relevantOffset,let lastControlEdgeValue):
                    make.bottom.equalToSuperview().offset(lastControlEdgeValue)
                    make.top.equalTo(imageView.snp.bottom).offset(relevantOffset)
                }
            }
            
        case .title_top_image_bottom(let titleTop,let imageSize,let layout):
            
            titleLabel.snp.makeConstraints { make in
                make.top.equalToSuperview().offset(titleTop)
                make.centerX.equalToSuperview()
            }
            
            imageView.snp.makeConstraints { make in
                
                make.size.equalTo(imageSize)
                make.centerX.equalToSuperview()
                
                switch layout{
                case .outside_stable(let lastControlEdgeValue):
                    make.bottom.equalToSuperview().offset(lastControlEdgeValue)
                case .inside_auto_resize(let relevantOffset,let lastControlEdgeValue):
                    make.bottom.equalToSuperview().offset(lastControlEdgeValue)
                    make.top.equalTo(titleLabel.snp.bottom).offset(relevantOffset)
                }
            }
        }
    }

//    override init(frame: CGRect) {
//        super.init(frame: frame)
//
//
//    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
//    MARK: - Pub Properties
    var titleLabel:UILabel = {
       let v = UILabel()
        return v
    }()
    
    var imageView:UIImageView = {
       let v = UIImageView()
        v.contentMode = .scaleAspectFill
        v.clipsToBounds = true
        return v
    }()
    
//    MARK:- private Properties
    
    fileprivate var backgroundColorStore = [UInt:UIColor]()
    
    fileprivate var imageAssetsStore = [UInt:ST_TianBtnImageAsset]()
    
    
    fileprivate var titleAttrStore = [UInt:NSAttributedString]()
    
    fileprivate lazy var states:[UIControl.State] = [.normal,.disabled,.highlighted,.selected]
    
    override var isHighlighted: Bool{
        didSet{
            binding_values()
        }
    }
    
    override var isSelected: Bool{
        didSet{
            binding_values()
        }
    }
    
    override var isEnabled: Bool{
        didSet{
            binding_values()
        }
    }
    
    
//    MARK:- private Method
    fileprivate func binding_values(){
        
        let key = self.state.rawValue
        
        
        if let asset = imageAssetsStore[key]{
            switch asset {
            case .image(let img):
                imageView.image = img
            case .url(let value):
                imageView.pin_setImage(from: value)
            }
        }else{
            imageView.image = nil
        }
        
        if let color = backgroundColorStore[key]{
            self.backgroundColor = color
        }
        
        if let attributedStr = titleAttrStore[key]{
            titleLabel.attributedText = attributedStr
        }
    }
    
}

//    MARK:- Extension Method
extension ST_TianButton{
    
    func setBackgroundColor(_ color: UIColor, for state: UIControl.State){
        backgroundColorStore.updateValue(color, forKey: state.rawValue)
        binding_values()
    }
    

    func setImage(_ image: UIImage?, for state: UIControl.State){
        
        if let img = image{
            imageAssetsStore.updateValue(ST_TianBtnImageAsset.image(img: img), forKey: state.rawValue)
            binding_values()
        }
        
    }
    
    func setImageUrl(_ imageUrl: URL?, for state: UIControl.State){
        imageAssetsStore.updateValue(ST_TianBtnImageAsset.url(value: imageUrl), forKey: state.rawValue)
        binding_values()
    }
    
    func setTitle(_ title: NSAttributedString, for state: UIControl.State){
        titleAttrStore.updateValue(title, forKey: state.rawValue)
        binding_values()
    }

    //    MARK:- Recommend public Method
    /// Set image for all states.
    ///
    /// - Parameter image: UIImage.
    func setImageForAllStates(_ image: UIImage) {
        
        states.forEach { setImage(image, for: $0) }
    }
    
    /// Set image url for all states.
    ///
    /// - Parameter imageUrl: URL.
    func setImageForAllStates(_ imageUrl: URL) {
        states.forEach { setImageUrl(imageUrl, for: $0) }
    }
    
}



